from jira import JIRA

url = "http://10.25.25.117:8080"
user = "195277"
password = "dingmiaobin"
projectName = "IPLATC"
mdPath = "C:\\nodeJSWorkspace\\xin3plat-control\\client\\MARKMAP.md";
levelSymbol = ["# ", "## ", "### ", "#### ", "##### ", "###### ","- "];


class Demand():
    def __init__(self, name, type):
        self.name = name;
        self.longName = "";
        self.url = "";
        self.type = type;
        self.children = [];
        self.parent = None;

    def __str__(self):
        return """{
            "name":""" + self.name + """,
            "longName":""" + self.longName + """,
            "url":""" + self.url + """,
            "type":""" + self.type + """,
            "children":""" + str(self.children) + """,
            "parent":""" + str(self.parent) + """
        })"""


def readMd(path):
    fileResult = []
    with open(path, 'r', encoding="utf-8") as file:
        linelist = file.readlines()
        for line in linelist:
            levelIndex = -1;
            for i in range(len(levelSymbol)):
                if line.startswith(levelSymbol[i]):
                    levelIndex = i;
                    break;
            if levelIndex > -1:
                content = line[line.index(levelSymbol[levelIndex]) + len(levelSymbol[levelIndex]):]
                content = content.replace("\n", "");
                fileResult.append([levelIndex, content]);
    return fileResult;


def buildDemand(mdResult):
    result = [];
    resultChild=[];
    tempNode = [];
    for i in range(len(mdResult) - 1):
        ni = i + 1;
        nodeStatue = -2;
        level = mdResult[i][0];
        name = mdResult[i][1];
        if ni == len(mdResult) - 1:
            nodeStatue = 1;
        elif mdResult[ni][0] == mdResult[i][0]:
            nodeStatue = 1;
        elif mdResult[ni][0] > mdResult[i][0]:
            nodeStatue = 0;
        elif mdResult[ni][0] < mdResult[i][0]:
            nodeStatue = -1;

        if nodeStatue == 1:
            demand = Demand(name, "故事");
            if level>=6:
                demand.type="子任务"
                demand.parent=result[-1];
                demand.longName=demand.name
                resultChild.append(demand);
                continue;
            demand.longName = ":".join(tempNode) + ":" + name;
            result.append(demand)
        elif nodeStatue == 0:
            tempNode.append(name);
        elif nodeStatue == -1:
            for x in range(level-mdResult[ni][0]):
                tempNode.pop();
    return [result,resultChild];


if __name__ == '__main__':

    jira = JIRA(server=url, basic_auth=(user, password))
    # boardName="XCYBER 看板"
    # sprintName="xCyber2021-Sprint5"
    project = jira.project(projectName)
    print(project)
    # project.
    # issueList = jira.search_issues("project = {}".format(projectName))
    # issueList = jira.search_issues("project = {} and issuetype = 故事".format(projectName))
    # issueList = jira.search_issues("project = {} and issuetype = 故事 and summary ~ \"{}\"".format(projectName, "上位机语言模型"))
    # print(issueList)
    t = buildDemand(readMd(mdPath))
    childTaskList=[]
    for x in t[0]:
        #添加故事
        jira.create_issue(project=projectName,summary=x.longName,description="",issuetype={'name':x.type})

    for x in t[1]:
        #添加子任务
        issueList = jira.search_issues(
            "project = {} and issuetype = 故事 and summary ~ \"{}\"".format(projectName, x.parent.longName))
        jira.create_issue(project=projectName, summary=x.name, description="", issuetype={'name': x.type},
                          parent={'key': issueList[0].key})

